package tr.gen._3bit.java._2k1s.dao.model;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validation;

@Entity
@Validation
public class Node extends Model {

	@Id
	@GeneratedValue
	private Integer id;
	private String identCode;
	@ManyToOne
	@JoinColumn(name = "serialInterfaceId")
	private SerialInterface serialInterface;
	private Integer divisor;
	private Integer timeInterval;
	@OneToMany(targetEntity = Measurement.class, mappedBy = "node", fetch = FetchType.LAZY)
	private List<Measurement> measurements;

	public List<Measurement> getMeasurements() {
		return measurements;
	}

	public void setMeasurements(List<Measurement> measurements) {
		this.measurements = measurements;
	}

	public SerialInterface getSerialInterface() {
		return serialInterface;
	}

	public void setSerialInterface(SerialInterface serialInterface) {
		this.serialInterface = serialInterface;
	}

	@IntRangeFieldValidator(message = "", key = "node.model.error.id.int", min = "0")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@RequiredStringValidator(message = "", key = "node.model.error.identCode.required" )
	@StringLengthFieldValidator(message = "", key = "node.model.error.identCode.length", minLength = "2", maxLength = "2")
	public String getIdentCode() {
		return identCode;
	}

	public void setIdentCode(String identCode) {
		this.identCode = identCode;
	}

	@RequiredFieldValidator(message = "", key = "node.model.error.divisor.required")
	@IntRangeFieldValidator(message = "", key = "node.model.error.divisor.int", min = "0")
	public Integer getDivisor() {
		return divisor;
	}

	public void setDivisor(Integer divisor) {
		this.divisor = divisor;
	}

	@IntRangeFieldValidator(message = "", key = "node.model.error.timeInterval.int", min = "0")
	public Integer getTimeInterval() {
		return timeInterval;
	}

	public void setTimeInterval(Integer timeInterval) {
		this.timeInterval = timeInterval;
	}
	
	@Transient
	public String getLabel() {
		return getIdentCode();
	}

}
